package xyz.bali16.module.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.bali16.module.article.entity.ArticleCategory;
import xyz.bali16.module.article.entity.ArticleTag;
import xyz.bali16.module.article.mapper.ArticleTagMapper;
import xyz.bali16.module.article.service.ArticleTagService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bali2 on 27/10/2021.
 * @version 1.0
 */
@Service
public class ArticleTagServiceImpl extends ServiceImpl<ArticleTagMapper, ArticleTag> implements ArticleTagService {
    @Autowired
    private ArticleTagMapper articleTagMapper;
    @Override
    public synchronized void addArticleTag(String articleId, List<String> tagIdsList) {
        List<ArticleTag> articleTags = new ArrayList<>();
        for (String tagId : tagIdsList) {
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(articleId);
            articleTag.setTagId(tagId);
            articleTags.add(articleTag);
        }
        Wrapper<ArticleTag> wrappers = Wrappers.<ArticleTag>lambdaQuery().eq(ArticleTag::getArticleId, articleId);
        remove(wrappers);

        saveBatch(articleTags);
    }

    @Override
    public List<ArticleTag> getByArticleId(String articleId) {
        Wrapper<ArticleTag> wrapper = Wrappers.<ArticleTag>lambdaQuery().eq(ArticleTag::getArticleId, articleId);
        return list(wrapper);
    }

    @Override
    public void deleteByArticleId(String articleId) {
        Wrapper<ArticleTag> wrapper = Wrappers.<ArticleTag>lambdaQuery().eq(ArticleTag::getArticleId, articleId);
        remove(wrapper);
    }

}
